import com.sun.jna.Pointer;

public class MainApp {


    public static void main(String[] args) {
        initSentry();

//        sendEvent();
        mockDivideZeroCrash();
//        mockJavaException();

        System.out.println("测试上报事件【done】");

        closeSentry();
        System.out.println("主进程结束...");
    }

    private static void initSentry() {
        Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
            @Override
            public void uncaughtException(Thread t, Throwable e) {
                e.printStackTrace();
                closeSentry();
            }
        });
        System.out.println("开始主进程...");
        Pointer options = CLibrary.INSTANCE.sentry_options_new();
//        System.out.println(options.dump(0, new CLibrary.SENTRY_OPTIONS().size()));
        System.out.println(options);
        System.out.println("创建 SentryOptions 对象【done】");
        CLibrary.INSTANCE.sentry_options_set_dsn(
                options,
                "http://334288d2996440b48f9bbf55d58e39dc@120.26.89.65:9000/3"
        );
        System.out.println("设置 SentryDSN 地址【done】");
        CLibrary.INSTANCE.sentry_options_set_handler_path(
                options,
                "C:\\Users\\Roy\\workspace\\crashDemo\\src\\crashpad_handler.exe"
        );
        System.out.println("设置 CrashpadHandler 地址【done】");
        CLibrary.INSTANCE.sentry_init(options);
        System.out.println("使用 SentryOptions 初始化【done】");
    }

    private static void closeSentry() {
        CLibrary.INSTANCE.sentry_close();
        System.out.println("SentryClose");
    }

    private static void sendEvent() {
        CLibrary.INSTANCE.sentry_capture_event(
                CLibrary.INSTANCE.sentry_value_new_message_event(
                        CLibrary.SENTRY_LEVEL.SENTRY_LEVEL_ERROR,
                        "ErrorLogger",
                        "This is an Error Msg." + System.currentTimeMillis()
                )
        );
    }

    private static void mockDivideZeroCrash() {
        System.loadLibrary("CrashDemo");
        System.out.println(new CrashDemo().testCrash());
    }

//    private static void mockNPECrash() {
//        System.loadLibrary("CrashDemo");
//        System.out.println(new CrashDemo().mockNPECrash());
//    }
//
//    private static void mockCustomException() {
//        System.loadLibrary("CrashDemo");
//        System.out.println(new CrashDemo().mockCustomException());
//    }

    private static void mockJavaException() {
        Object test = null;
        System.out.println(test.toString());
    }

}
